package cn.flying.cloud.tool.ops.instance.controller;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.tool.ops.instance.entity.InstanceInfo;
import cn.flying.cloud.tool.ops.instance.service.InstanceInfoService;

/**
 * 实例信息表 前端控制器
 *
 * @author: admin
 * @date: 2025-04-25 23:11:44
 * @version: 1.0
 */
@RestController
@RequestMapping("/tool/ops/instance")
public class InstanceInfoController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private InstanceInfoService instanceInfoService;

    /**
     * 分页查询实例信息表数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    public PageResponse<InstanceInfo> page(@RequestBody PageRequest<InstanceInfo> request) {
        return instanceInfoService.getPageList(request);
    }

    /**
     * 列表查询实例信息表数据
     *
     * @param instanceInfo
     * @return
     */
    @PostMapping("list")
    public Rt<?> list(@RequestBody InstanceInfo instanceInfo) {
        List<InstanceInfo> list = instanceInfoService.getList(instanceInfo);
        return Rt.success(list);
    }

    /**
     * 查询实例信息表详情数据
     *
     * @param instanceInfo
     * @return
     */
    @PostMapping("detail")
    public Rt<?> detail(InstanceInfo instanceInfo) {
        return Rt.success(instanceInfoService.getOneObj(instanceInfo));
    }

    /**
     * 根据主键ID查询实例信息表详情数据
     *
     * @param id 主键ID
     * @return
     */
    @PostMapping("getById")
    public Rt<?> getById(Long id) {
        InstanceInfo instanceInfo = instanceInfoService.getById(id);
        return Rt.success(instanceInfo);
    }

    /**
     * 保存实例信息表数据
     *
     * @param instanceInfo
     * @return
     */
    @PostMapping("save")
    public Rt<?> save(@RequestBody InstanceInfo instanceInfo) {
        if (Objects.isNull(instanceInfo.getId())) {
            // 新增
            instanceInfoService.createObj(instanceInfo);
        } else {
            // 更新
            instanceInfoService.updateObj(instanceInfo);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除实例信息表数据
     *
     * @param instanceInfo
     * @return
     */
    @PostMapping("delete")
    public Rt<?> delete(InstanceInfo instanceInfo) {
        instanceInfoService.deleteObj(instanceInfo);
        return Rt.success("删除成功！");
    }

    /**
     * 根据主键ID删除实例信息表数据
     *
     * @param id 主键ID
     * @return
     */
    @GetMapping("deleteById/{id}")
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        instanceInfoService.deleteById(id);
        return Rt.success("删除成功！");
    }
}
